// SpcoPersist.cpp: implementation of the SpcoPersist class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SpcoPersist.h"
#include "Spco.h"
#include "DatabaseBase.h"
#include "DatabaseElement.h"
#include "SelectorTable.h"
#include "Spco.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace PDMS::SpecFrame;

namespace PDMS
{
	namespace PDMSDBIF
	{
		BEGIN_REGISTER_MAP( SpcoPersist, PersistRegBase )
		REGISTER( UE_RUNTIME_CLASS( Spco ), GET_REGISTER( PersistRegBase, VersionOne ) )
		END_REGISTER_MAP();

		SpcoPersist::SpcoPersist()
		{

		}

		SpcoPersist::~SpcoPersist()
		{

		}

		UESoft::ErrorStatus SpcoPersist::Retrieve( Persistable*& pObj )
		{
			ASSERT( pObj );
			
			UESoft::ErrorStatus es = PersistRegBase::ReadPaternity( pObj );
			if ( es != UESoft::eOk )
			{
				return es;
			}
			
			DatabaseBase* pDatabase = NULL;
			pDatabase = this->GetDatabase();
			ASSERT( pDatabase );

			DatabaseElement* pElement = NULL;
			pElement = pDatabase->GetDatabaseElement( _T("selector") );
			ASSERT( pElement );

			ASSERT( pElement->IsKindOf( UE_RUNTIME_CLASS( SelectorTable ) ) );
			SelectorTable* pSelectorTable = static_cast<SelectorTable*>( pElement );

			ASSERT( pObj->IsKindOf( UE_RUNTIME_CLASS( Spco ) ) );
			Spco* pSpco = static_cast<Spco*>( pObj );

			StdString strCondition;
			strCondition.Format( _T("ID = %ld"), pObj->Id().ObjectID() );

			es = pSelectorTable->Open( strCondition );
			if ( es != UESoft::eOk )
			{
				return es;
			}
			
			pSpco->SetSpecName( pSelectorTable->GetSpecName() );
			pSpco->Name( pSelectorTable->GetName() );
			pSpco->SetBltref( pSelectorTable->GetBoltDir() );
			pSpco->SetBltID( pSelectorTable->GetBOLTID() );
			pSpco->SetCatref( pSelectorTable->GetCATAMDBDir() );
			pSpco->SetCatID( pSelectorTable->GetSCOMID() );
			pSpco->SetCmpref( pSelectorTable->GetComponentDir() );
			pSpco->SetCmpID( pSelectorTable->GetComponentID() );
			pSpco->SetMatxt( pSelectorTable->GetMTEXID() );
			pSpco->SetDetID( pSelectorTable->GetSDTEID() );
			pSpco->SetSType( pSelectorTable->GetSType() );
			pSpco->SetPbor0( pSelectorTable->GetPbor0() );
			pSpco->SetPbor1( pSelectorTable->GetPbor1() );
			pSpco->SetPbor2( pSelectorTable->GetPbor2() );
			pSpco->SetPbor3( pSelectorTable->GetPbor3() );
			pSpco->SetGType( pSelectorTable->GetGType() );
			pSpco->SetEdge( pSelectorTable->GetEdge() );
			pSpco->SetAllo( pSelectorTable->GetAllo() );
			pSpco->SetAngle( pSelectorTable->GetAngle() );
			pSpco->SetShop( pSelectorTable->GetShop() );
			
			pSelectorTable->Close();
			return UESoft::eOk;
		}

		UESoft::ErrorStatus SpcoPersist::Create( Persistable* pObj )
		{
			ASSERT( pObj );
			
			UESoft::ErrorStatus es = PersistRegBase::CreatePaternity( pObj );
			if ( es != UESoft::eOk )
			{
				return es;
			}
			
			DatabaseBase* pDatabase = NULL;
			pDatabase = this->GetDatabase();
			ASSERT( pDatabase );

			DatabaseElement* pElement = NULL;
			pElement = pDatabase->GetDatabaseElement( _T("selector") );
			ASSERT( pElement );

			ASSERT( pElement->IsKindOf( UE_RUNTIME_CLASS( SelectorTable ) ) );
			SelectorTable* pSelectorTable = static_cast<SelectorTable*>( pElement );

			ASSERT( pObj->IsKindOf( UE_RUNTIME_CLASS( Spco ) ) );
			Spco* pSpco = static_cast<Spco*>( pObj );
			
			es = pSelectorTable->Open();
			if ( es != UESoft::eOk )
			{
				return es;
			}
			pSelectorTable->NewRecord();
			pSelectorTable->SetID( pSpco->Id().ObjectID() );
			pSelectorTable->SetName( pSpco->Name() );
			pSelectorTable->SetSpecName( pSpco->GetSpecName() );
			pSelectorTable->SetBoltDir( pSpco->GetBltref() );
			pSelectorTable->SetBOLTID( pSpco->GetBltID() );
			pSelectorTable->SetCATAMDBDir( pSpco->GetCatref() );
			pSelectorTable->SetSCOMID( pSpco->GetCatID() );
			pSelectorTable->SetComponentDir( pSpco->GetCmpref() );
			pSelectorTable->SetComponentID( pSpco->GetCmpID() );
			pSelectorTable->SetMTEXID( pSpco->GetMatxtID() );
			pSelectorTable->SetSDTEID( pSpco->GetDetID() );
			pSelectorTable->SetSType( pSpco->GetSType() );
			pSelectorTable->SetPbor0( pSpco->GetPbor0() );
			pSelectorTable->SetPbor1( pSpco->GetPbor1() );
			pSelectorTable->SetPbor2( pSpco->GetPbor2() );
			pSelectorTable->SetPbor3( pSpco->GetPbor3() );
			pSelectorTable->SetGType( pSpco->GetGType() );
			pSelectorTable->SetEdge( pSpco->GetEdge() );
			pSelectorTable->SetAllo( pSpco->GetAllo() );
			pSelectorTable->SetAngle( pSpco->GetAngle() );
			pSelectorTable->SetShop( pSpco->GetShop() );
			
			return UESoft::eOk;
		}

		UESoft::ErrorStatus SpcoPersist::Update( Persistable* pObj )
		{
			ASSERT( pObj );
			DatabaseBase* pDatabase = NULL;
			pDatabase = this->GetDatabase();
			ASSERT( pDatabase );

			DatabaseElement* pElement = NULL;
			pElement = pDatabase->GetDatabaseElement( _T("selector") );
			ASSERT( pElement );

			ASSERT( pElement->IsKindOf( UE_RUNTIME_CLASS( SelectorTable ) ) );
			SelectorTable* pSelectorTable = static_cast<SelectorTable*>( pElement );

			ASSERT( pObj->IsKindOf( UE_RUNTIME_CLASS( Spco ) ) );
			Spco* pSpco = static_cast<Spco*>( pObj );
			
			UESoft::ErrorStatus es = pSelectorTable->Open();
			if ( es != UESoft::eOk )
			{
				return es;
			}

			pSelectorTable->UpdateRecord( pSpco->Id().ObjectID());
			pSelectorTable->SetName( pSpco->Name() );
			pSelectorTable->SetSpecName( pSpco->GetSpecName() );
			pSelectorTable->SetBoltDir( pSpco->GetBltref() );
			pSelectorTable->SetBOLTID( pSpco->GetBltID() );
			pSelectorTable->SetCATAMDBDir( pSpco->GetCatref() );
			pSelectorTable->SetSCOMID( pSpco->GetCatID() );
			pSelectorTable->SetComponentDir( pSpco->GetCmpref() );
			pSelectorTable->SetComponentID( pSpco->GetCmpID() );
			pSelectorTable->SetMTEXID( pSpco->GetMatxtID() );
			pSelectorTable->SetSDTEID( pSpco->GetDetID() );
			pSelectorTable->SetSType( pSpco->GetSType() );
			pSelectorTable->SetPbor0( pSpco->GetPbor0() );
			pSelectorTable->SetPbor1( pSpco->GetPbor1() );
			pSelectorTable->SetPbor2( pSpco->GetPbor2() );
			pSelectorTable->SetPbor3( pSpco->GetPbor3() );
			pSelectorTable->SetGType( pSpco->GetGType() );
			pSelectorTable->SetEdge( pSpco->GetEdge() );
			pSelectorTable->SetAllo( pSpco->GetAllo() );
			pSelectorTable->SetAngle( pSpco->GetAngle() );
			pSelectorTable->SetShop( pSpco->GetShop() );
			
			return UESoft::eOk;
		}
	}
}


